package cn.tyoui.UI;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.List;

import cn.tyoui.tool.HDFSFile;
import cn.tyoui.tool.IP;
import cn.tyoui.tool.UPData;

public class UI extends javax.swing.JFrame {
    private javax.swing.JButton createDir;
    private javax.swing.JButton deleteDir;
    private javax.swing.JButton deleteFile;
    private javax.swing.JButton flush;
    private javax.swing.JTextField hdfsDir;
    private javax.swing.JTextField hdfsFile;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelName;
    private javax.swing.JButton localDir;
    private javax.swing.JButton localFile;
    private javax.swing.JTextField newDir;
    private javax.swing.JButton reboot;
    private javax.swing.JTextField rmFile;
    private javax.swing.JTextField rmdir;
    private javax.swing.JTextArea scanFileName;
    private javax.swing.JButton scanLive;
    private javax.swing.JTextArea scanNode;
    private javax.swing.JButton uplandDir;
    private javax.swing.JButton uploadFile;
    private static final long serialVersionUID = 1L;
    private HDFSFile hdfsFileList;
    private UPData data;

    public UI() {
        Diag("请使用局域网");
        data = new UPData();

        initComponents();
    }

    private void initComponents() {
        labelName = new javax.swing.JLabel();
        flush = new javax.swing.JButton();
        reboot = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        scanFileName = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        newDir = new javax.swing.JTextField("");
        createDir = new javax.swing.JButton();
        rmFile = new javax.swing.JTextField("");
        deleteFile = new javax.swing.JButton();
        rmdir = new javax.swing.JTextField("");
        deleteDir = new javax.swing.JButton();
        hdfsFile = new javax.swing.JTextField("");
        uploadFile = new javax.swing.JButton();
        hdfsDir = new javax.swing.JTextField("");
        uplandDir = new javax.swing.JButton();
        scanNode = new javax.swing.JTextArea();
        scanLive = new javax.swing.JButton();
        localFile = new javax.swing.JButton();
        localDir = new javax.swing.JButton();
        scanFileName.setEditable(false);
        scanNode.setEditable(false);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelName.setFont(new java.awt.Font("宋体", 0, 18));
        labelName.setText("Hadoop管理程序");

        flush.setText("刷新");

        reboot.setText("重启");

        scanFileName.setColumns(20);
        scanFileName.setRows(5);
        jScrollPane1.setViewportView(scanFileName);

        jLabel2.setText("文件目录");

        createDir.setText("新建文件夹");

        deleteFile.setText("删除文件");

        deleteDir.setText("删除文件夹");

        uploadFile.setText("上传文件");

        uplandDir.setText("上传文件夹");

        scanLive.setText("查看活节点");

        localFile.setText("本地文件");

        localDir.setText("本地文件夹");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout
                                .createParallelGroup(
                                        javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(
                                        layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18,
                                                        18)
                                                .addGroup(layout
                                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(rmFile).addComponent(rmdir).addComponent(scanNode)
                                                        .addComponent(newDir,
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                                layout.createSequentialGroup()
                                                                        .addGroup(layout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(localFile,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        Short.MAX_VALUE)
                                                                                .addGroup(layout.createSequentialGroup()
                                                                                        .addComponent(localDir)
                                                                                        .addGap(0, 0, Short.MAX_VALUE)))
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addGroup(layout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                                        false)
                                                                                .addComponent(hdfsDir,
                                                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        91, Short.MAX_VALUE)
                                                                                .addComponent(hdfsFile,
                                                                                        javax.swing.GroupLayout.Alignment.TRAILING))))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout
                                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(layout
                                                                        .createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                false)
                                                                        .addComponent(createDir,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(deleteFile,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(deleteDir,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(uploadFile,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE))
                                                                .addComponent(uplandDir))
                                                        .addComponent(scanLive))
                                                .addGap(29, 29, 29))
                                .addGroup(layout.createSequentialGroup().addGap(54, 54, 54)
                                        .addComponent(flush, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(79, 79, 79)
                                        .addComponent(labelName, javax.swing.GroupLayout.PREFERRED_SIZE, 209,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(42, 42, 42).addComponent(reboot).addGap(0, 0, Short.MAX_VALUE))))
                .addGroup(layout
                        .createSequentialGroup().addGap(117, 117, 117).addComponent(jLabel2,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                .createSequentialGroup().addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(labelName)
                        .addComponent(reboot).addComponent(flush))
                .addGap(1, 1, 1)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 19,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout
                        .createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(newDir, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(createDir))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(rmFile, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(deleteFile))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(rmdir, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(deleteDir))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(hdfsFile, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(uploadFile).addComponent(localFile))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(hdfsDir, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(uplandDir).addComponent(localDir))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(scanNode, javax.swing.GroupLayout.PREFERRED_SIZE, 158,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(scanLive)))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();

        flush.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                scanFileName.setText("");
                hdfsFileList = new HDFSFile("hdfs://" + IP.MASTER_IP + ":9000/");
                List<String> list = hdfsFileList.getList();
                if (list.size() == 0) {
                    Diag("HDFS链接失败！请查看链接");
                    return;
                }
                for (String string : list) {
                    scanFileName.append(string + "\n");
                }
                Diag("扫描完毕！");
            }
        });

        createDir.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String dirPath = newDir.getText().trim();
                if (dirPath.equals(""))
                    return;
                try {
                    boolean b = data.mkdir(dirPath);
                    if (b) {
                        Diag("创建成功！");
                    } else
                        Diag("创建失败！");
                    newDir.setText("");
                    newDir.requestFocus();
                } catch (Exception e1) {
                }
            }
        });

        deleteFile.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String deString = rmFile.getText().toString();
                if (deString.equals(""))
                    return;
                try {
                    boolean b = data.deleteHDFSFile(deString);
                    if (b)
                        Diag("删除成功");
                    else
                        Diag("删除失败");
                } catch (IOException e1) {
                }
                rmFile.setText("");
            }
        });

        deleteDir.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String dir = rmdir.getText().toString();
                if (dir.equals(""))
                    return;
                try {
                    if (data.deleteDir(dir))
                        Diag("删除文件夹成功！");
                    else
                        Diag("删除文件夹失败！");
                } catch (IOException e1) {
                }
                rmdir.setText("");
            }
        });

        uploadFile.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String localPath = OpenFile.path;
                String hdString = hdfsFile.getText().toString();
                if (localPath.equals("") || hdString.equals(""))
                    return;
                try {
                    data.upload(localPath, hdString);
                    Diag("上传成功！");
                } catch (IOException e1) {
                    Diag("上传失败！");
                }
                localFile.setText("");
                hdfsFile.setText("");
            }
        });

        localFile.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                OpenFile file = new OpenFile();
                file.startFile();
                file.close();
            }
        });

        localDir.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                OpenFile file = new OpenFile();
                file.startFile();
                file.close();
            }
        });

        uplandDir.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String localDirpath = OpenFile.path;
                String hdfspath = hdfsDir.getText().trim();
                if (localDirpath.equals("") || hdfspath.equals(""))
                    return;
                try {
                    data.uploadDir(localDirpath, hdfspath);
                    Diag("上传文件夹成功！");
                } catch (IOException e1) {
                    Diag("上传失败！");
                }
                hdfsDir.setText("");
            }
        });

        scanLive.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    List<String> list = data.getDateNodeHost();
                    String str = "";
                    for (String string : list) {
                        str += string + "\n";
                    }
                    scanNode.setText(str);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
    }

    public void Diag(String string) {
        Frame frame = new Frame();
        Dialog dialog = new Dialog(frame, "提示信息");
        dialog.setBounds(400, 200, 350, 150);
        Label label = new Label();
        label.setText("\t" + string);
        dialog.add(label);
        dialog.setVisible(true);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dialog.setVisible(false);
            }
        });
    }

    public static void main(String args[]) {
        new UI().setVisible(true);

    }
}
